package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FeedBackListEntity extends BaseEntity {

	private static final long serialVersionUID = -7297446194764688277L;

	public List<FeedBackEntity> dataList = null;

	public String statusCode = "";
	public String msg = "";

	public String phone = "";
	public final static String PHONE = "phone";

	public final static String STATUS_CODE = "statusCode";
	public final static String MSG = "msg";

	private final static String FEED_BACKS = "feedbacks";

	public static FeedBackListEntity parse(JSONObject jsonObject)
			throws JSONException {
		FeedBackListEntity bean = new FeedBackListEntity();
		if (jsonObject.has(STATUS_CODE)) {
			bean.statusCode = jsonObject.getString(STATUS_CODE);
		}
		if (jsonObject.has(MSG)) {
			bean.msg = jsonObject.getString(MSG);
		}
		if (jsonObject.has(PHONE)) {
			bean.phone = jsonObject.getString(PHONE);
		}
		/**
		 * 解析feedbacks节点
		 */
		if (jsonObject.has(FEED_BACKS)) {

			JSONArray jsonArray = jsonObject.getJSONArray(FEED_BACKS);

			bean.dataList = new ArrayList<FeedBackEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				FeedBackEntity entity = FeedBackEntity.parse(object);
				bean.dataList.add(entity);
			}

		}

		if (jsonObject.has("opencases")) {
			JSONArray jsonArray = jsonObject.getJSONArray("opencases");
			bean.dataList = new ArrayList<FeedBackEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				FeedBackEntity entity = FeedBackEntity.parse(object);
				bean.dataList.add(entity);
			}
		}
		return bean;
	}

	public static class FeedBackEntity extends BaseEntity {
		private static final long serialVersionUID = 3307012525680511212L;

		public static final String PIC_FILE_SEPRATOR = ",";
//		public String barcode = "";
		public String id = "";
		public String content = "";
		public String answerContent = "";
		public String title = "";
		public String answerName = "";
		public String seriesName = "";
		public String submitDate = "";
		public String score = "";
		public String videoPath = "";// 视频路径
		public String picPathes = "";// 图片文件路径 用^号隔开
		public String uploadFilePath = "";// 上传的图片路径

		public List<String> picNames = new ArrayList<String>();
		public List<FeedBackMessageEntity> msgs = new ArrayList<FeedBackMessageEntity>();

		public final static String ID = "id";
		public final static String CONTENT = "content";
		public final static String ANSWER_CONTENT = "answerContent";
		public final static String TITLE = "title";
		public final static String ANSWER_NAME = "answerName";
		public final static String SERIES_NAME = "seriesName";
		public final static String SUBMIT_DATE = "submitDate";
		public final static String PIC_NAMES = "picNames";
		public final static String MESSAGES = "messages";
		public final static String SCORE = "score";

		public static FeedBackEntity parse(JSONObject jsonObject)
				throws JSONException {
			FeedBackEntity bean = new FeedBackEntity();
			if (jsonObject.has(ID)) {
				bean.id = jsonObject.getString(ID);
			}
			if (jsonObject.has(CONTENT)) {
				bean.content = jsonObject.getString(CONTENT);
			}

			if (jsonObject.has(ANSWER_CONTENT)) {
				bean.answerContent = jsonObject.getString(ANSWER_CONTENT);
			}
			if (jsonObject.has(TITLE)) {
				bean.title = jsonObject.getString(TITLE);
			}
			if (jsonObject.has(ANSWER_NAME)) {
				bean.answerName = jsonObject.getString(ANSWER_NAME);
			}
			if (jsonObject.has(SERIES_NAME)) {
				bean.seriesName = jsonObject.getString(SERIES_NAME);
			}
			if (jsonObject.has(SUBMIT_DATE)) {
				bean.submitDate = jsonObject.getString(SUBMIT_DATE);
			}
			if (jsonObject.has(SCORE)) {
				bean.score = jsonObject.getString(SCORE);
			}
			if (jsonObject.has(PIC_NAMES)) {
				JSONArray jsonArray = jsonObject.getJSONArray(PIC_NAMES);
				bean.picNames = new ArrayList<String>();
				for (int i = 0; i < jsonArray.length(); i++) {
					if (!"".equals(jsonArray.getString(i).trim())) {
						bean.picNames.add(jsonArray.getString(i));
					}
				}
			}
			if (jsonObject.has(MESSAGES)) {
				JSONArray jsonArray = jsonObject.getJSONArray(MESSAGES);
				if (jsonArray != null && jsonArray.length() > 0) {
					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject jo = jsonArray.getJSONObject(i);
						bean.msgs.add(FeedBackMessageEntity.parse(jo));
					}
				}

			}

			return bean;
		}
	}
}
